package com.mianyang.zhangsan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mianyang.zhangsan.common.constant.SecConstant;
import com.mianyang.zhangsan.common.util.DateHelper;
import com.mianyang.zhangsan.common.util.OperationManHelper;
import com.mianyang.zhangsan.common.util.StringUtils;
import com.mianyang.zhangsan.common.util.VideoUtils;
import com.mianyang.zhangsan.exception.SecServiceException;
import com.mianyang.zhangsan.model.dto.SecVideoDto;
import com.mianyang.zhangsan.model.dto.UploadData;
import com.mianyang.zhangsan.model.entity.SecVideo;
import com.mianyang.zhangsan.plugin.oss.MinioService;
import com.mianyang.zhangsan.service.ISecFileService;
import com.mianyang.zhangsan.service.SecStaticDataService;
import com.mianyang.zhangsan.service.SecVideoService;
import com.mianyang.zhangsan.mapper.SecVideoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
* @author jiangjr
*/
@Service
public class SecVideoServiceImpl extends ServiceImpl<SecVideoMapper, SecVideo>
    implements SecVideoService{
    @Autowired
    private ISecFileService fileService;

    @Override
    public Page<SecVideo> selectVideos(SecVideoDto cond, int current, int pageSize) {
        QueryWrapper<SecVideo> wrapper = new QueryWrapper<>();
        if (cond != null) {
            if (StringUtils.isNotEmpty(cond.getVideoName())) {
                wrapper.like("video_name", cond.getVideoName());

            }
            if (StringUtils.isNotEmpty(cond.getAccountName())) {
                wrapper.like("account_name", cond.getAccountName());
            }
            if (cond.getHitTop() != null && cond.getHitTop()) {
                wrapper.orderBy(true, false, "hit");
            }
            if (cond.getLaterTop() != null && cond.getLaterTop()) {
                wrapper.orderBy(true, true, "upload_time");
            }
            if (cond.getEarlierYear() != null && cond.getEarlierYear()) {
                Date currentYear = DateHelper.parseDate(cond.getCreateTimeYear(), "yyyy");
                wrapper.lt("upload_time", currentYear);
            } else {
                if (StringUtils.isNotEmpty(cond.getCreateTimeYear())) {
                    Date currentYear = DateHelper.parseDate(cond.getCreateTimeYear(), "yyyy");
                    Date nextYear = DateHelper.nextYear(cond.getCreateTimeYear());
                    wrapper.between("upload_time", currentYear, nextYear);
                }
            }
            if (cond.getVideoType() != null) {
                wrapper.eq("video_type", cond.getVideoType());
            }
        }
        return page(new Page<>(current, pageSize), wrapper);
    }

    @Override
    public List<SecVideo> selectVideosOrderBy(String column, int top) {
        QueryWrapper<SecVideo> wrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(column)) {
            column = "hit";
        }
        wrapper.orderBy(true, false, column);
        Page<SecVideo> videoPage = page(new Page<>(1, top), wrapper);
        return videoPage.getRecords();
    }

    @Override
    public void uploadVideo(MultipartFile videoFile, SecVideo video, int uploadType) {
        UploadData videoData = null;
        if (uploadType == 1) {
            if (videoFile == null) {
                throw new SecServiceException("请选择上传的视频");
            }
            // 上传视频文件
            videoData = fileService.uploadFile(videoFile, SecConstant.VIDEO_FILE_TYPE);
            long videoTime = VideoUtils.getVideoDuration(videoFile);
            video.setFileId(videoData.getFileId());
            video.setVideoSrc(videoData.getUploadPath());
            video.setAccountId(OperationManHelper.getAccountId());
            video.setAccountName(OperationManHelper.getAccountName());
            video.setVideoImg(SecConstant.DEFAULT_VIDEO_IMG);
            video.setScore(0d);
            video.setHit(0d);
            video.setAvgScore(0d);
            video.setAppraiseNum(0d);
            video.setUploadTime(new Date());
            video.setVideoTime(videoTime);
            save(video);
        } else {
            if (videoFile != null) {
                videoData = fileService.uploadFile(videoFile, SecConstant.IMG_FILE_TYPE);
                video.setVideoImg(videoData.getUploadPath());
            }
            updateById(video);
        }
    }

    @Override
    public void appraise(Long videoId, double score) {
        SecVideo video = getById(videoId);
        // 计算总共得分
        double total = video.getScore() + score;
        double appraiseNum = video.getAppraiseNum() + 1d;
        // 计算平均份
        double avgScore = Math.round(total / appraiseNum);
        // 更新数据
        video.setScore(total);
        video.setAppraiseNum(appraiseNum);
        video.setAvgScore(avgScore);
        updateById(video);
    }

    @Override
    public void addHitNum(Long videoId) {
        SecVideo video = getById(videoId);
        double hit = video.getHit();
        video.setHit(hit + 1d);
        updateById(video);
    }
}




